namespace Lumos.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;
    using Lumos.Models;
    using Microsoft.AspNet.Identity;
    using Microsoft.AspNet.Identity.EntityFramework;

    internal sealed class Configuration : DbMigrationsConfiguration<Lumos.Models.ApplicationDbContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
            ContextKey = "Lumos.Models.ApplicationDbContext";
        }

        protected override void Seed(Lumos.Models.ApplicationDbContext context)
        {
            //create AppAdmin role if not exists
            if (!context.Roles.Any(r => r.Name == "AppAdmin"))
            {

                //create AppAdmin role
                var roleStore = new RoleStore<IdentityRole>(context);
                var roleManager = new RoleManager<IdentityRole>(roleStore);

                roleManager.Create(new IdentityRole { Name = "AppAdmin" });

            }

            //set AppAdmin user if not exists
            if (!context.Users.Any(u => u.UserName == "appadmin@lumos.local"))
            {
                //create AppAdmin user
                var userStore = new UserStore<ApplicationUser>(context);
                var userManager = new UserManager<ApplicationUser>(userStore);
                var appAdminUser = new ApplicationUser { UserName = "appadmin@lumos.local", Email = "appadmin@lumos.local" };

                userManager.Create(appAdminUser, "AppAdmin@Lumos");

                //add AppAdmin user to AppAdmin role
                userManager.AddToRole(appAdminUser.Id, "AppAdmin");

            }
            
            
        }
    }
}
